//
//  ReservationRecordViewController.m
//  homeland
//
//  Created by SunShine on 13-10-31.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ReservationRecordViewController.h"
#import "AdvisoryListCell.h"

@interface ReservationRecordViewController ()
@property (strong, nonatomic) ReservationService *dataService;
@end

@implementation ReservationRecordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"预约结果";
    self.dataService = [ReservationService new];
    _dataService.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Value Buttons Action
- (IBAction)button1Action:(id)sender
{
    _image1View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    [_dataService voteForReservationWithID:_listItem.Id AndVote:1];
    [self startLoadingViewWithText:@"正在提交..."];
}

- (IBAction)button2Action:(id)sender
{
    _image1View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    [_dataService voteForReservationWithID:_listItem.Id AndVote:2];
    [self startLoadingViewWithText:@"正在提交..."];
}

- (IBAction)button3Action:(id)sender
{
    _image1View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image2View.image = [UIImage imageNamed:@"RadioButton-Unselected.png"];
    _image3View.image = [UIImage imageNamed:@"RadioButton-selected.png"];
    [_dataService voteForReservationWithID:_listItem.Id AndVote:3];
    [self startLoadingViewWithText:@"正在提交..."];
}

#pragma mark - Table Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AdvisoryListCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"AdvisoryListCell" owner:self options:nil] objectAtIndex:0];
    if (indexPath.row == 0) {
        cell.codeLabel.text = @"预约编号";
        cell.statusLabel.text = _listItem.reserveCode;
    } else if (indexPath.row == 1) {
        cell.codeLabel.text = @"预约项";
        cell.statusLabel.text = _listItem.businessName;
    } else if (indexPath.row == 2) {
        cell.codeLabel.text = @"预约人";
        cell.statusLabel.text = _listItem.name;
    } else if (indexPath.row == 3) {
        cell.codeLabel.text = @"预约人手机";
        cell.statusLabel.text = _listItem.mobileNum;
    } else if (indexPath.row == 4) {
        cell.codeLabel.text = @"预约人身份证";
        cell.statusLabel.text = _listItem.idcardNum;
    } else if (indexPath.row == 4) {
        cell.codeLabel.text = @"预约人身份证";
        cell.statusLabel.text = _listItem.idcardNum;
    } else if (indexPath.row == 5) {
        cell.codeLabel.text = @"预约日期";
        if (_listItem.reserveDate && _listItem.reserveDate != (id)[NSNull null]) {
            cell.statusLabel.text = _listItem.reserveDate;
        } else {
            cell.statusLabel.text = @"";
        }
    } else if (indexPath.row == 6) {
        cell.codeLabel.text = @"预约开始时间";
        cell.statusLabel.text = _listItem.reserveStime;
    } else if (indexPath.row == 7) {
        cell.codeLabel.text = @"预约结束时间";
        cell.statusLabel.text = _listItem.reserveEtime;
    } else if (indexPath.row == 8) {
        cell.codeLabel.text = @"受理状态";
        if (_listItem.reserveStatus && _listItem.reserveStatus != (id)[NSNull null] && _listItem.reserveStatus.integerValue == 1) {
            cell.statusLabel.text = @"已回复";
        } else {
            cell.statusLabel.text = @"未回复";
        }
    } else if (indexPath.row == 9) {
        cell.codeLabel.text = @"受理部门";
        cell.statusLabel.text = _listItem.unitName;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - Data Service Delegate
-(void)loadDataFinished
{
    [self stopLoadingView];
    _button1.enabled = NO;
    _button2.enabled = NO;
    _button3.enabled = NO;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"反馈成功." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
@end
